<template>
  <div>
<!--    <h1>管理员管理</h1>-->
    <div style="padding: 10px 0">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入用户名或电话" v-model="key"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div style="padding: 10px 0">
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
<!--      <el-popconfirm-->
<!--          class="ml-5"-->
<!--          confirm-button-text='好的'-->
<!--          cancel-button-text='不用了'-->
<!--          icon="el-icon-info"-->
<!--          icon-color="red"-->
<!--          title="确定批量删除吗？"-->
<!--          @confirm="delBatch"-->
<!--      >-->
<!--        <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline"></i></el-button>-->
<!--      </el-popconfirm>-->
<!--      <el-upload action="http://localhost:8081/user/import" accept="xlsx" :show-file-list="false" :on-success="handExcelImportSuccess"-->
<!--           :on-error="handExcelError"      style="display: inline-block">-->
<!--      <el-button type="primary" class="ml-5">导入<i class="el-icon-bottom"></i></el-button>-->
<!--      </el-upload>-->
<!--      <el-button type="primary" @click="exp" class="ml-5">导出<i class="el-icon-top"></i></el-button>-->
    </div>



    <el-table :data="tableDate" border stripe :header-cell-class-name="headerBg" @selection-change="handleSelectionChange" >
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>


      <el-table-column prop="id" label="Id" width="140"></el-table-column>
      <el-table-column prop="username" label="用户账户" width="120"></el-table-column>
      <el-table-column prop="phone" label="电话号码"></el-table-column>
      <el-table-column prop="createTime" label="用户创建时间"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="warning" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='好的'
              cancel-button-text='不用了'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除这个管理员吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

<!--    分页设置-->
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

<!--    添加用户用-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="from.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input  v-model="from.password" autocomplete="off"
          style="" ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input  v-model="from.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "User",
  data() {
    return {
      //模糊查询用

      key:"",
      //表格数据
      tableDate: [],
      //数据总量
      total: 0,
      //当前页
      pageNum: 1,
      //一页有多少数据
      pageSize: 5,
      //控制dialog是否可见
      dialogFormVisible: false,
      //dialog数据绑定
      from: {},
      //多重选择
      multipleSelection: [],
      headerBg: 'headerBg',
    }
  },
  created() {
    this.load();
  },
  methods: {
    //加载数据方法
    load(){

      // 1 使用axios进行操作 这里的request在utils中定义 其中引入了Axios
      // 2 //在request.js中设置了baseURL
      request.get("/Administration/user/page",{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          key:this.key
        }
      }).then(res=>{
        console.log(res)
        this.tableDate=res.data.records
        this.total=res.data.total
      })


    },

    //当每一页数据多少改变时调用
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageSize=pageSize
      this.load()
    },
    //当前页改变时调用
    handleCurrentChange(pageNum){
      console.log(pageNum)
      this.pageNum=pageNum
      this.load()
    },
    //重置查询数据
    reset(){
      this.pageNum=1,
          this.pageSize=5,
          this.key=""
      //重置数据后调用查询方法
      this.load()
    },
    //点击添加按钮的时候弹出dialog
    handleAdd(){
      this.dialogFormVisible = true
      //把from变为空
      this.from={}
    },
    //编辑数据方法
    handleEdit(row){

      //深拷贝操作
      // this.from.username = JSON.parse(JSON.stringify(row.username))
      // this.from.id = JSON.parse(JSON.stringify(row.id))
      // this.from.phone = JSON.parse(JSON.stringify(row.phone))
      row.password=''
      this.from=JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
    },
    //table多选框方法  当你选择多选框时会返回数据给val
    handleSelectionChange(val){
      console.log(val)
      this.multipleSelection = val
    },
    // //批量删除方法
    // delBatch(){
    //   let ids = this.multipleSelection.map(v => v.id) //把对象变为id的数组 [{},{},{}] => [1,2,3]
    //   request.post("/admin/del/user/batch",ids).then(res=>{
    //     if (res.status === 10000){
    //       this.$message.success("批量删除成功")
    //       this.load()
    //       this.dialogFormVisible = false
    //     }else {
    //       this.$message.error("批量删除失败")
    //     }
    //   })
    // },
    //单个删除方法
    del(id){
      request.delete("Administration/user/delete/" + id).then(res=>{
        if (res.status === 10000){
          this.$message.success("删除成功")
          this.load()
          this.dialogFormVisible = false
        }else {
          this.$message.error("删除失败")
          }
      })
    },
    //导出数据方法
    // exp(){
    //   window.open("http://localhost:8085/admin/user/export")
    // },
    //如果导入成功
    handExcelImportSuccess(){
      this.$message.success("导入成功")
      //刷新数据
      this.load()
    },
    //如果导入失败
    handExcelError(){
      this.$message.error("只支持xlsx文件,并且上传时确保没有ID列")
    },
    //提交数据方法 新增和修改
    save(){
      // 1 使用axios进行操作 这里的request在utils中定义 其中引入了Axios
      //2 在request.js中设置了baseURL
      request.post("/Administration/user/save",this.from).then(res=>{
        //调用后端接口返回res为true的话则成功
        if (res.status === 10000){
          this.$message.success("保存成功")
          this.from={}
          this.load()
          this.dialogFormVisible = false
        }
        else{
          this.$message.error("保存失败")
        }
      })
    }
  }
}
</script>

<style>
.headerBg{
  background: #eee !important;
}
</style>
